<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<title> MobileAgents2 &lt; Mundo &lt; Foswiki</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="icon" href="/foswiki/pub/System/ProjectLogos/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/foswiki/pub/System/ProjectLogos/favicon.ico" type="image/x-icon" />
<link rel="alternate" href="http://127.0.0.1/foswiki/bin/edit/Mundo/MobileAgents2?t=1295995547" type="application/x-wiki" title="edit MobileAgents2" />
<meta name="TEXT_NUM_TOPICS" content="Number of topics:" />
<meta name="TEXT_MODIFY_SEARCH" content="Modify search" />
<meta name="robots" content="noindex" /><link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/foswiki/bin/view/Mundo/WebRss" />
<style type="text/css" media="all">
@import url('style/base.css');
</style>
<style type="text/css" media="all">
@import url('style/layout.css');
@import url('style/style.css');
@import url('style/colors.css');
</style>
<style type="text/css" media="all">
@import url('style/column_left.css');
@import url('style/variant_foswiki_noframe.css');
</style><style type="text/css" media="all">
	/* Styles that are set using variables */
	.patternBookView .foswikiTopRow,
	.patternWebIndicator a img,
	.patternWebIndicator a:hover img {
		background-color:#efefef;
	}
	#patternTopBarContents { background-image:url(/foswiki/pub/System/PatternSkin/header5.gif); background-repeat:repeat-x;}
#patternTopBarContents { background-color:#ffffff;}
.patternBookView {
	border-color:#efefef;
}
.patternPreviewPage #patternMain {
	/* uncomment to set the preview image */
	/*background-image:url("/foswiki/pub/System/PreviewBackground/preview2bg.gif");*/
}
</style>
<style type="text/css" media="all">
@import url('style/print.css');
</style>
<!--[if IE]><style type="text/css" media="screen">
pre {
	height:1%;
	overflow-x:auto;
}
.foswikiAttachments,
.foswikiForm,
.foswikiHelp,
.foswikiPreviewArea,
.patternPreviewPage .foswikiForm,
.patternSigLine,
.patternToolBar,
.patternTop,
.patternTopicAction,
#patternSideBarContents .patternLeftBarPersonal,
#patternSideBarContents h2,
#patternSideBarContents li,
#patternTopBarButtons ul {
	height:1%;
}
#patternSideBarContents .patternLeftBarPersonal {
	width:100%;
}
.foswikiFormStep {
	height:100%;
}
#foswikiLogin,
.patternShadow {
	border:10px solid #fff;
	margin-top:10px;
	margin-bottom:10px;
	border:2px solid #ccc;
}
</style><![endif]-->
<!--[if gt IE 8 ]><style type="text/css" media="screen">
#foswikiLogin,
.patternShadow {
	border:10px solid #fff;
	margin-top:10px;
	margin-bottom:10px;
	box-shadow: 0 0 10px #ccc;
}
</style><![endif]-->
<meta name="foswiki.TWISTYANIMATIONSPEED" content="fast" /><!--TWISTYPLUGIN::META-->
<style type="text/css" media="all">
.foswikiTable {border-width:1px}
.foswikiTable .tableSortIcon img {padding-left:.3em; vertical-align:text-bottom}
.foswikiTable td {border-style:solid none; vertical-align:top}
.foswikiTable th {border-style:none solid; vertical-align:top; background-color:#d6d3cf; color:#000000}
.foswikiTable th a:link {color:#000000}
.foswikiTable th a:visited {color:#000000}
.foswikiTable th a:hover {color:#000000; background-color:#d6d3cf}
.foswikiTable th.foswikiSortedCol {background-color:#c4c1ba}
.foswikiTable tr.foswikiTableRowdataBg0 td {background-color:#ffffff}
.foswikiTable tr.foswikiTableRowdataBg0 td.foswikiSortedCol {background-color:#f7f7f6}
.foswikiTable tr.foswikiTableRowdataBg1 td {background-color:#f7f7f6}
.foswikiTable tr.foswikiTableRowdataBg1 td.foswikiSortedCol {background-color:#f0f0ee}
</style><!--TABLEPLUGIN_default-->
<meta name="foswiki.PUBURL" content="http://127.0.0.1/foswiki/pub" /> <!-- PUBURL -->
<meta name="foswiki.PUBURLPATH" content="/foswiki/pub" /> <!-- PUBURLPATH -->
<meta name="foswiki.SCRIPTSUFFIX" content="" /> <!-- SCRIPTSUFFIX -->
<meta name="foswiki.SCRIPTURL" content="http://127.0.0.1/foswiki/bin" /> <!-- SCRIPTURL -->
<meta name="foswiki.SCRIPTURLPATH" content="/foswiki/bin" /> <!-- SCRIPTURLPATH -->
<meta name="foswiki.SERVERTIME" content="25%20Jan%202011%20-%2023:45" /> <!-- SERVERTIME -->
<meta name="foswiki.SKIN" content="pattern" /> <!-- SKIN -->
<meta name="foswiki.SYSTEMWEB" content="System" /> <!-- SYSTEMWEB -->
<meta name="foswiki.TOPIC" content="MobileAgents2" /> <!-- TOPIC -->
<meta name="foswiki.USERNAME" content="guest" /> <!-- USERNAME -->
<meta name="foswiki.USERSWEB" content="Main" /> <!-- USERSWEB -->
<meta name="foswiki.WEB" content="Mundo" /> <!-- WEB -->
<meta name="foswiki.WIKINAME" content="WikiGuest" /> <!-- WIKINAME -->
<meta name="foswiki.WIKIUSERNAME" content="Main.WikiGuest" /> <!-- WIKIUSERNAME -->
<meta name="foswiki.NAMEFILTER" content="%5b%5cs%5c*%3f~%5e%5c%24%40%25%60%22'%26%3b%7c%3c%3e%5c%5b%5c%5d%23%5cx00-%5cx1f%5d" /> <!-- NAMEFILTER --><!--JQUERYPLUGIN::FOSWIKI::META-->
</head>
<body class="patternViewPage patternPrintPage">
<a name="PageTop"></a><div class="foswikiPage"><div id="patternScreen">
<div id="patternPageShadow">
<div id="patternPage">
<div id="patternOuter">
<div id="patternFloatWrap">
<div id="patternMain">
<div id="patternMainContents">
<div class="patternContent"><div class="foswikiTopic"> <h1><a name="Communicating_With_Local_Services"></a>  Communicating With Local Services </h1>
<p />
Previous: <a href="MobileAgents1.html">Creating Autonomous Agents</a>
<p />
Any nontrivial agent will have to communicate with other services or agents to do its task. This part of the tutorial will show two things:
<p /> <ul>
<li> How to obtain references to local services running on the same node and how to communicate with them.
</li> <li> How to obtain information about other nodes currently present in the MundoCore overlay network.
</li></ul> 
<p />
<h3><a name="api"></a>  api </h3>
<p />
Like in the examples before, <code>IMyAgent</code> specifies the interface of the agent, which will be used by the master.
<p />
<strong>samples/agent/agent2/src/api/IMyAgent</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> api;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.annotation.mcRemote;

@mcRemote
<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">interface</FONT></B> IMyAgent {
  <B><FONT COLOR="#A020F0">void</FONT></B> run();
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
In this example, the agent will visit a server and communicate with it. The interface <code>IMyServer</code> defines the interface of the server that will be used by the agent.
<p />
<strong>samples/agent/agent2/src/api/IMyServer</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> api;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.annotation.mcRemote;

@mcRemote
<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">interface</FONT></B> IMyServer {
  <B><FONT COLOR="#A020F0">void</FONT></B> step1();
  <B><FONT COLOR="#A020F0">void</FONT></B> step2();
  <B><FONT COLOR="#A020F0">void</FONT></B> step3();
  <B><FONT COLOR="#A020F0">void</FONT></B> step4();
  <B><FONT COLOR="#A020F0">void</FONT></B> step5();
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="agent"></a>  agent </h3>
<p />
When the agent is started through the <code>run</code> method, it first determines the server to which it will move. In the previous examples, the name of the destination node was hardcoded. Here, the agent uses <code>Node.getNeighbors()</code> to obtain a list of the neighbor nodes. The agent then chooses the first item of the list and moves to this node.
<p />
Arrived at the server, the agent uses <code>Mundo.Mundo.getServiceByType</code> to obtain a reference to a service that implements the <code>IMyServer</code> interface. After that, it calls five methods on the server and finally migrates back to the master.
<p />
This example illustrates a frequently cited feature of mobile agents: Instead of performing multiple (slow) remote calls to a server, we move the agent to the server and perform the calls there locally.
<p />
<strong>samples/agent/agent2/src/agent/MyAgent</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.Agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.annotation.mcSerialize;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.service.Node;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.rt.Mundo;
<B><FONT COLOR="#A020F0">import</FONT></B> api.IMyAgent;
<B><FONT COLOR="#A020F0">import</FONT></B> api.IMyServer;

@mcSerialize
<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">class</FONT></B> MyAgent <B><FONT COLOR="#A020F0">extends</FONT></B> Agent <B><FONT COLOR="#A020F0">implements</FONT></B> IMyAgent {
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> run() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** starting at &quot;</FONT></B>+Node.thisNode().getName());

    Node[] nodes = Node.getNeighbors();
    <B><FONT COLOR="#A020F0">if</FONT></B> (nodes.length &lt; 1)
      <B><FONT COLOR="#A020F0">throw</FONT></B> <B><FONT COLOR="#A020F0">new</FONT></B> IllegalStateException(<B><FONT COLOR="#BC8F8F">&quot;no peers present&quot;</FONT></B>);
    moveTo(nodes[0].getName(), <B><FONT COLOR="#BC8F8F">&quot;atServer1&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> atServer1() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** now at &quot;</FONT></B>+Node.thisNode().getName());

    IMyServer srv = (IMyServer)Mundo.getServiceByType(IMyServer.<B><FONT COLOR="#A020F0">class</FONT></B>);
    <B><FONT COLOR="#A020F0">if</FONT></B> (srv == <B><FONT COLOR="#A020F0">null</FONT></B>)
      <B><FONT COLOR="#A020F0">throw</FONT></B> <B><FONT COLOR="#A020F0">new</FONT></B> IllegalStateException(<B><FONT COLOR="#BC8F8F">&quot;server service not found!&quot;</FONT></B>);
    srv.step1();
    srv.step2();
    srv.step3();
    srv.step4();
    srv.step5();

    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** leaving &quot;</FONT></B>+Node.thisNode().getName());
    moveTo(<B><FONT COLOR="#BC8F8F">&quot;master&quot;</FONT></B>, <B><FONT COLOR="#BC8F8F">&quot;atMaster&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> atMaster() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;*** back at &quot;</FONT></B>+Node.thisNode().getName());
  }
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="server"></a>  server </h3>
<p />
The server implementation just prints to console, when a method is called.
<p />
<strong>samples/agent/agent2/src/server/MyServer</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> server;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.util.DefaultApplication;
<B><FONT COLOR="#A020F0">import</FONT></B> api.IMyServer;

<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">class</FONT></B> MyServer <B><FONT COLOR="#A020F0">extends</FONT></B> DefaultApplication <B><FONT COLOR="#A020F0">implements</FONT></B> IMyServer {
  <B><FONT COLOR="#A020F0">public</FONT></B> MyServer() {
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> step1() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;step1&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> step2() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;step2&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> step3() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;step3&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> step4() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;step4&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> step5() {
    System.out.println(<B><FONT COLOR="#BC8F8F">&quot;step5&quot;</FONT></B>);
  }
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">static</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> main(String args[]) {
    start(<B><FONT COLOR="#A020F0">new</FONT></B> MyServer());
  }
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="app"></a>  app </h3>
<p />
The following implementation of <code>MyApp</code> creates an instance of the agent and runs it.
<p />
<strong>samples/agent/agent2/src/app/MyApp</strong>
<!-- SyntaxHighlightingPlugin --><pre class='syntaxHighlightingPlugin'><B><FONT COLOR="#A020F0">package</FONT></B> app;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.rt.Mundo;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.rt.Service;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.Agent;
<B><FONT COLOR="#A020F0">import</FONT></B> org.mundo.agent.DoIMobility;
<B><FONT COLOR="#A020F0">import</FONT></B> api.DoIMyAgent;

<B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">class</FONT></B> MyApp {
  <B><FONT COLOR="#A020F0">public</FONT></B> <B><FONT COLOR="#A020F0">static</FONT></B> <B><FONT COLOR="#A020F0">void</FONT></B> main(String args[]) {
    Mundo.init();
    <B><FONT COLOR="#A020F0">try</FONT></B> {
      Service client = <B><FONT COLOR="#A020F0">new</FONT></B> Service();
      Mundo.registerService(client);
      DoIMobility mobility = Agent.newInstance(client.getSession(), <B><FONT COLOR="#BC8F8F">&quot;agent.MyAgent&quot;</FONT></B>);
      DoIMyAgent agent = <B><FONT COLOR="#A020F0">new</FONT></B> DoIMyAgent(mobility);
      agent.run();
    }
    <B><FONT COLOR="#A020F0">catch</FONT></B>(Exception x) {
      x.printStackTrace();
    }
    Mundo.shutdown();
  }
}</pre><!-- end SyntaxHighlightingPlugin -->
<p />
<h3><a name="Running_the_example"></a>  Running the example </h3>
<p />
The example uses two MundoCore nodes, named <code>master</code> and <code>server</code>. Their working directories reside in <code>var/master</code> and <code>var/server</code>, respectively. The buildfile will generate the runscripts <code>run-server</code> and <code>run-master</code> to start these nodes. To run the example, start <code>run-server</code> and then <code>run-master</code>.
<p /> <ul>
<li> <strong>server</strong> runs the server service <strong>MyServer</strong>.
</li> <li> <strong>master</strong> runs the application <strong>MyApp</strong>.
</li></ul> 
</div></div><!-- /patternContent-->
</div></div></div></div></div></div></div></div>
</body></html>
